<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "addr".
 *
 * @property integer $addrid
 * @property integer $user_id
 * @property string $name
 * @property string $phone
 * @property string $qq
 * @property string $alipay
 * @property string $aliname
 * @property string $province
 * @property string $city
 * @property string $county
 * @property string $detail
 * @property integer $state
 */
class Addr extends \yii\db\ActiveRecord
{
    const STATE_DEFAULT = 1;
    const STATE_NOT_DEFAULT = 0;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'addr';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'name', 'phone', 'qq', 'alipay', 'aliname', 'province', 'city', 'county', 'detail'], 'required'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'addrid' => 'id',
            'user_id' => '用户id',
            'name' => '姓名',
            'phone' => '电话',
            'qq' => 'qq',
            'alipay' => '支付宝账号',
            'aliname' => '支付宝真实姓名',
            'province' =>'省',
            'city' => '市',
            'county' => '区',
            'detail' => '详细地址',
            'state' => '状态',
        ];
    }

    public function setDefault()
    {
        $user_id = $this->user_id;
        $addrid = $this->addrid;
        $this->state = self::STATE_DEFAULT;
        $this->save(false);
        self::updateAll(['state'=>self::STATE_NOT_DEFAULT],['and',['user_id'=>$user_id],['<>','addrid',$addrid]]);
    }
}
